package com.insurance.modules.claim.controller;

import com.insurance.modules.app.utils.LoginSysUserContext;
import com.insurance.modules.sys.entity.SysUserEntity;
import com.insurance.modules.sys.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.InetAddress;
import java.net.UnknownHostException;

@Slf4j
public class BaseController {


    protected Log logger = LogFactory.getLog(this.getClass());

    @Autowired
    protected HttpServletRequest request;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 获取当前请求
     */
    public HttpServletRequest getRequest() {
        return request;
    }

    /**
     * 获取当前会话
     *
     * @param isNew true:session不存在则创建
     * @return
     */
    public HttpSession getSession(boolean isNew) {
        if (request != null) {
            return request.getSession(isNew);
        }
        return null;
    }

    /**
     * 使用指定的名称将对象绑定到该会话
     *
     * @param key   Name of session
     * @param value Value of session
     */
    public void setSession(String key, String value) {
        getSession(true).setAttribute(key, value);
    }

    /**
     * 获取项目相对路径
     */
    public String getProjectPath() {
        return "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
    }

    /**
     * 获取项目物理路径
     */
    public String getProjectMirPath() {
        return getSession(true).getServletContext().getRealPath("/");
    }

    /**
     * 获取访问客户端IP
     */
    public String getClientIP() {
        if (request == null) {
            return null;
        }
        String ipAddress = null;
        try {
            ipAddress = request.getHeader("x-forwarded-for");
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if (ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")) {
                    // 根据网卡取本机配置的IP
                    InetAddress inet = null;
                    try {
                        inet = InetAddress.getLocalHost();
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                    assert inet != null;
                    ipAddress = inet.getHostAddress();
                }
            }
            // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
            if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
                // = 15
                if (ipAddress.indexOf(",") > 0) {
                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ipAddress;
    }

    public SysUserEntity getSysUser() {
        Long userId = getUserId();
        return getSysUserById(userId);
    }

    public SysUserEntity getSysUserById(Long userId) {
        if (userId == null) {
            return null;
        }
        try {
            return sysUserService.getById(userId);
        } catch (Exception e) {
            logger.error("查询用户信息失败", e);
            return null;
        }
    }

    public Long getUserId() {
        try {
            return LoginSysUserContext.getLoginUserId();
        } catch (Exception e) {
            logger.error("获取用户id失败", e);
            return null;
        }
    }

   /* public SysUserEntity getSysUser() {
        String userToken = getRequest().getHeader("userToken");
        try {
            String userId = this.getUserId(userToken);
            if (userId == null) {
                return null;
            }
            return sysUserService.getById(userId);
        } catch (Exception e) {
            logger.error("查询用户信息失败", e);
            return null;
        }
    }
    public String getSysUserId() {
        SysUserEntity sysUser = getSysUser();
        return sysUser == null ? null : String.valueOf(sysUser.getUserId());
    }

    public String getUserId(String userToken) {
        String userId;
        try {
            if(!JwtOperatorUtil.validateToken(userToken)){
                log.error("【校验用户凭证失败, 用户凭证错误】");
                return null;
            }
            userId = JwtOperatorUtil.getUserId(userToken);
            if(StringUtils.isBlank(userId)){
                log.error("【获取用户主键失败, 用户为空】");
                return null;
            }
            return userId;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }*/

}